package com.llkj.qingnianguoxue.mine.mycoin;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MyScoreRecordAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.CostRecordListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 使用明细
 */
public class CoinListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView lvRecord;
    private MyScoreRecordAdapter recordAdapter;
    private List<CostRecordListBean.CostRecordBean> recordeData;
    private int totalSize, mPage, mCurrentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cost_record);
        setTitle("使用明细", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        ButterKnife.bind(this);

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvRecord.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        recordeData = new ArrayList<>();
        recordAdapter = new MyScoreRecordAdapter(R.layout.item_cost_record, recordeData);
        lvRecord.setAdapter(recordAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        recordAdapter.setOnLoadMoreListener(this);
        lvRecord.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if(view.getId()== R.id.tvDelItem){
                    mCurrentPosition = i;
                    dialogConfirm("您要删除该记录吗？", null, "取消", "删除").show();
                }
            }
        });
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        CostRecordListBean.CostRecordBean recordBean = recordeData.get(mCurrentPosition);
        loadWaitDialog();
        HttpMethod.delIntegral(this, this, recordBean.getId());
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getScoreData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getScoreData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, recordAdapter, null, lvRecord, totalSize);
    }

    /**
     * 获取积分记录
     */
    private void getScoreData() {
        HttpMethod.userIntegral(this, this, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);

        if (method == HttpMethod.HTTP_USER_INTEGRAL) {//积分记录
            CostRecordListBean recordListBean = GsonUtil.GsonToBean(data, CostRecordListBean.class);
            if (recordListBean != null) {
                totalSize = Integer.parseInt(recordListBean.getNum());
                if (mPage == 1) {
                    recordeData.clear();
                    LoadDataHelper.refreshComplete(recordAdapter, recordeData, refreshLayout, lvRecord);
                } else {
                    recordAdapter.setEnableLoadMore(true);
                    recordAdapter.loadMoreComplete();
                }
                recordeData.addAll(recordListBean.getDetails());
                recordAdapter.notifyDataSetChanged();
            }
        }if (method == HttpMethod.HTTP_USER_DEL_INTEGRAL) {//删除积分记录
            CostRecordListBean recordListBean = GsonUtil.GsonToBean(data, CostRecordListBean.class);
            if (recordListBean != null) {
                recordAdapter.remove(mCurrentPosition);
            }
        }
    }

}
